package com.xthink.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xthink.common.result.AjaxResult;
import com.xthink.common.pojo.polo.PoolAndPackage;
import com.xthink.common.pojo.polo.PoolPackage;
import com.xthink.common.vo.pool.PoolAndPackageLikeListVo;
import com.xthink.web.mapper.PoolAndPackageMapper;
import com.xthink.web.mapper.PoolPackageMapper;
import com.xthink.web.service.PoolAndPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PoolAndPackageServiceImpl extends ServiceImpl<PoolAndPackageMapper, PoolAndPackage> implements PoolAndPackageService {

    @Autowired
    private PoolAndPackageMapper poolAndPackageMapper;

    @Autowired
    private PoolPackageMapper poolPackageMapper;

    //流量池内部绑定池套餐查询
    @Override
    public AjaxResult listPoolPackage(PoolAndPackageLikeListVo poolAndPackageLikeListVo) {
        LambdaQueryWrapper<PoolAndPackage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PoolAndPackage::getPoolId,poolAndPackageLikeListVo.getPoolId());
        List<PoolAndPackage> poolAndPackages = poolAndPackageMapper.selectList(wrapper);
        ArrayList<PoolPackage> poolPackages = new ArrayList<>();
        for (PoolAndPackage poolAndPackage : poolAndPackages) {
            Integer poolPackageId = poolAndPackage.getPoolPackageId();
            PoolPackage poolPackage = poolPackageMapper.selectById(poolPackageId);
            poolPackages.add(poolPackage);
        }
        return AjaxResult.success(poolPackages);
    }

    //解绑套餐回显
    @Override
    public AjaxResult unBinDingPoolPackage(Integer poolId) {
        LambdaQueryWrapper<PoolAndPackage> poolAndPackageLambdaUpdateWrapper = new LambdaQueryWrapper<>();
        poolAndPackageLambdaUpdateWrapper.eq(PoolAndPackage::getPoolId,poolId);
        poolAndPackageLambdaUpdateWrapper.eq(PoolAndPackage::getDelFlag,0);
        List<PoolAndPackage> poolAndPackages = poolAndPackageMapper.selectList(poolAndPackageLambdaUpdateWrapper);
        if (poolAndPackages.size()==0){
            return AjaxResult.error("该流量池暂未绑定套餐");
        }
        ArrayList<PoolPackage> poolPackages = new ArrayList<>();
        for (PoolAndPackage poolAndPackage : poolAndPackages) {
            Integer poolPackageId = poolAndPackage.getPoolPackageId();
            PoolPackage poolPackage = poolPackageMapper.selectById(poolPackageId);
            poolPackages.add(poolPackage);
        }
        return AjaxResult.success(poolPackages);
    }
}
